package cn.edu.lingnan.tourism.service.Impl;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.mapper.UserMapper;
import cn.edu.lingnan.tourism.pojo.User;
import cn.edu.lingnan.tourism.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
@Transactional(rollbackFor = TourismException.class)
    @Override
    public int insertUser(User user) throws TourismException {
    try{
        int flag = userMapper.insertSelective(user);
        if(flag==1){
            return flag;
        }else {
            throw new TourismException("系统已有该用户");
        }
    }catch (Exception e){
        throw new TourismException("注册失败");
    }

    }
@Transactional(readOnly = true)
    @Override
    public User selectUserByUsernameAndPassword(User user) throws TourismException {
    try{
        User login = userMapper.selectOne(user);
        if(login!=null){
            return login;
        }else {
            throw new TourismException("无该用户");
        }
    }catch (Exception e){
        throw new TourismException("登录失败");
    }

    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public int updateUser(User user) throws TourismException {
    try{
        int flag = userMapper.updateByPrimaryKeySelective(user);
        if(flag==1){
            return flag;
        }else {
            throw new TourismException("该用户已被删除或无该用户");
        }
    }catch (Exception e){
        throw new TourismException("修改失败");
    }

    }
@Transactional(readOnly = true)
    @Override
    public User getUserByUid(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }
    @Override
    public List<User> getUserByType(User user) {
        return userMapper.select(user);
    }

}
